home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 899 < prev    next >
Encoding:
Text File  |  1996-08-06  |  4.5 KB  |  110 lines

  1. Path: chronicle.mti.sgi.com!austern
  2. From: davew@trigati.cs.haverford.edu (David G. Wonnacott)
  3. Newsgroups: comp.std.c++,gnu.g++.help
  4. Subject: Rationale behind disallowal of non-const reference to rvalue
  5. Followup-To: comp.std.c++
  6. Date: 28 Mar 1996 09:35:40 PST
  7. Organization: Bryn Mawr and Haverford College NetNews
  8. Approved: austern@isolde.mti.sgi.com
  9. Message-ID: <DAVEW.96Mar27195129@trigati.cs.haverford.edu>
  10. NNTP-Posting-Host: isolde.mti.sgi.com
  11. X-Original-Date: 28 Mar 1996 00:51:29 GMT
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBVAwUBMVrN7Ey4NqrwXLNJAQGEEwIAycfmWzC8dDNJTp821V+aDwzb4GxUoKmw
  14.     KnYvi00Q5Qc5jc4dk+vDU83+iDoi3EA+KTIEj/E+aWYf8vMrjejwUg==
  15.     =w784
  16. Originator: austern@isolde.mti.sgi.com
  17.  
  18. [Moderator's note: this article is crossposted to comp.std.c++ and
  19. gnu.g++.help, and followups have been set to comp.std.c++.  Please be
  20. aware of what newsgroup your article is going to when you respond.  mha]
  21.  
  22. I am looking for an explanation for the rationale behind the decision
  23. of the standards committee to disallow the binding of a non-const
  24. reference to an rvalue.  I assume this has been discussed to death
  25. here before, but I haven't been able to find it in a FAQ; I'd be happy
  26. with a reference to another document rather than a full reply here.
  27.  
  28. The only answer I've seen for this question before is that it is
  29. disallowed because the reference may outlive the rvalue it refers to.
  30. But isn't this just as much a risk for const references as non-const?
  31. I can't see why non-const references should be illegal when const
  32. references are legal.
  33.  
  34. One other question (for the g++ group) - is the current plan for G++
  35. to eventually give an error, rather than a warning, when this rule is
  36. broken?  If so, is there any possibility of a switch to disable this?
  37.  
  38.  
  39.  
  40. A detailed explanation of my question, and references, follow.  Those
  41. of you who have seen this before may want to skip the rest.
  42.  
  43. Paragraph 2 of Section 13.3.3.1.4 of the April draft of the C++
  44. standard (http://www.cygnus.com/misc/wp/draft/, repeated below for
  45. convenience) states that the following is illegal (at least as far as
  46. I understand it):
  47.  
  48.     class foo { ... };
  49.     foo f1();
  50.     int f2(foo &f);
  51.  
  52.     void test()
  53.     {
  54.         int i = f2(f1());
  55.     }
  56.  
  57. g++ agrees with my reading, giving the following warning for this code:
  58.  
  59.   In function `void test()':
  60.   warning: initialization of non-const `foo &' from rvalue `foo'
  61.   warning: in passing argument 1 of `f2(foo &)'
  62.  
  63. However, if f2 is changed to "foo f2(const foo &f);", there is no
  64. problem (either according to my reading of the standard or to g++).
  65.  
  66. So - my questions are:
  67.  
  68. 1) What can go wrong with f2 as it is written that can't go wrong with
  69. f2 if it took a const foo &?  I know that the reference could out-live
  70. the compiler generated temporary - but that can happen with the
  71. "legal" code as well.
  72.  
  73. 2) If there is no error that would be prevented by the introduction of
  74. const, why is the code legal with const and not without?  Just because
  75. nobody thought it would be useful to do this?  We do it all over the
  76. place in the Omega Library (http://www.cs.umd.edu/projects/omega) - we
  77. want to perform an operation that extracts information from the
  78. argument of f2, and return the extracted information.  Whats the big
  79. deal if this operation may have a side effect?
  80.  
  81. Puzzled,
  82.     Dave Wonnacott
  83.     davew@cs.haverford.edu
  84.  
  85.  
  86. -------  Relevant sections of http://www.cygnus.com/misc/wp/draft/ -------
  87.  
  88.   13.3.3.1.4  Reference binding                           [over.ics.ref]
  89.  
  90. 1 The operation of binding a reference is not a conversion, but for  the
  91.   purposes of overload resolution it is considered to be part of a stan-
  92.   dard conversion sequence (specifically, it is the last step in such  a
  93.   sequence).
  94.  
  95. 2 A standard conversion sequence cannot be formed if it requires binding
  96.   a reference to non-const to an rvalue (except when binding an implicit
  97.   object   parameter;   see   the   special   rules  for  that  case  in
  98.   _over.match.funcs_).  [Note: this means, for example, that a candidate
  99.   function  cannot  be a viable function if it has a non-const reference
  100.   parameter (other than the implicit object parameter)  and  the  corre-
  101.   sponding argument is a temporary or would require one to be created to
  102.   initialize the reference (see _dcl.init.ref_).  ]
  103. ---
  104. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  105.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  106.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  107.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  108.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  109. ]
  110.